//
//  IntersectionCalloutContentView.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 10/17/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "IntersectionCalloutContentView.h"

@implementation IntersectionCalloutContentView

@synthesize mapPoint=_mapPoint;
@synthesize viewController=_viewController;

- (void)dealloc
{
    [_mapPoint release];
    [_viewController release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrameAndAnnoation:(CGRect)frame annoation:(IntersectionMapPoint*) annoation viewController:(UIViewController *)viewController
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.mapPoint=annoation;
        self.viewController=viewController;
        
        UILabel * titleLabel=[[[UILabel alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-10, 20)] autorelease];
        titleLabel.text=self.mapPoint.intersection.intersectionName;
        titleLabel.textAlignment =  UITextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
        
        UILabel * subTitleLabel=[[[UILabel alloc] initWithFrame:CGRectMake(5, 5+titleLabel.frame.origin.y+titleLabel.frame.size.height, frame.size.width-10, 5)] autorelease];
        subTitleLabel.text=self.mapPoint.subTitle;
        subTitleLabel.textAlignment =  UITextAlignmentCenter;
        subTitleLabel.textColor = [UIColor whiteColor];
        subTitleLabel.backgroundColor = [UIColor clearColor];
        subTitleLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
        
        [self addSubview:titleLabel];
        [self addSubview:subTitleLabel];
        
        NSArray * timingPatternList=[XMLParser getTimingPatternList:self.mapPoint.agency.agencyID subSystemID:self.mapPoint.subSystem.subSystemID];
        for (int i=0; i<timingPatternList.count; i++) {
            TimingPattern *timingPattern=[timingPatternList objectAtIndex:i];
            CalloutContentviewTimingPatternButton *timingPatternButton = [CalloutContentviewTimingPatternButton buttonWithType:UIButtonTypeCustom];
            timingPatternButton.timingPattern=timingPattern;
            [timingPatternButton setFrame:CGRectMake(0, 10+20*i+subTitleLabel.frame.origin.y+subTitleLabel.frame.size.height, frame.size.width-20, 20)];
            [timingPatternButton setTitle:timingPattern.timingPatternName forState:UIControlStateNormal];
            timingPatternButton.titleLabel.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
            timingPatternButton.backgroundColor=[UIColor clearColor];
            timingPatternButton.titleLabel.backgroundColor=[UIColor clearColor];
            [timingPatternButton.layer setBorderColor:[[UIColor grayColor] CGColor]];
            [timingPatternButton.layer setBorderWidth:2.0];
            [timingPatternButton addTarget:self action:@selector(clickTimingPattern:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:timingPatternButton];

//            [timingPatternButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
//            [timingPatternButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
//            [self.view addSubview:sampleButton];
            
            
//            UILabel * timePatternLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 10+20*i+subTitleLabel.frame.origin.y+subTitleLabel.frame.size.height, frame.size.width-10, 20)];
//            timePatternLabel.text=timingPattern.timingPatternName;
//            timePatternLabel.textAlignment =  UITextAlignmentCenter;
//            timePatternLabel.textColor = [UIColor whiteColor];
//            timePatternLabel.backgroundColor = [UIColor clearColor];
//            timePatternLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
//            [self addSubview:timePatternLabel];
        }
        
        
        
//        UILabel * dateStampLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 5+(frame.size.height-10)*2/6, frame.size.width-10, (frame.size.height-10)/6)];
//        
//        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
//        [formatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
        
        
//        dateStampLabel.text=[formatter stringFromDate:self.mapPoint.rWIS.dateTimeStamp];
//        dateStampLabel.textAlignment =  UITextAlignmentCenter;
//        dateStampLabel.textColor = [UIColor whiteColor];
//        dateStampLabel.backgroundColor = [UIColor clearColor];
//        dateStampLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
        
//        UILabel * tempratureLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 5+(frame.size.height-10)*3/6,frame.size.width-10, (frame.size.height-10)/6)];
//        tempratureLabel.text=[NSString stringWithFormat:@"Air Temperature: %@ F",self.mapPoint.rWIS.airTemprature];
//        tempratureLabel.textAlignment =  UITextAlignmentLeft;
//        tempratureLabel.textColor = [UIColor whiteColor];
//        tempratureLabel.backgroundColor = [UIColor clearColor];
//        tempratureLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
//        
//        UILabel * humidityLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 5+(frame.size.height-10)*4/6, frame.size.width-10, (frame.size.height-10)/6)];
//        humidityLabel.text=[NSString stringWithFormat:@"Relative Humidity: %@ %%",self.mapPoint.rWIS.relativeHumidity];
//        humidityLabel.textAlignment =  UITextAlignmentLeft;
//        humidityLabel.textColor = [UIColor whiteColor];
//        humidityLabel.backgroundColor = [UIColor clearColor];
//        humidityLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
//        
//        UILabel * dewPointLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 5+(frame.size.height-10)*5/6,frame.size.width-10,  (frame.size.height-10)/6)];
//        dewPointLabel.text=[NSString stringWithFormat:@"DewPoint: %@ F",self.mapPoint.rWIS.dewPoint];
//        dewPointLabel.textAlignment =  UITextAlignmentLeft;
//        dewPointLabel.textColor = [UIColor whiteColor];
//        dewPointLabel.backgroundColor = [UIColor clearColor];
//        dewPointLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(12.0)];
        
        
        
//        [self addSubview:dateStampLabel];
//        [self addSubview:tempratureLabel];
//        [self addSubview:humidityLabel];
//        [self addSubview:dewPointLabel];
        //        [self setBackgroundColor:[UIColor greenColor]];
    }
    
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (IBAction)clickTimingPattern:(id)sender
{
    CalloutContentviewTimingPatternButton *button=(CalloutContentviewTimingPatternButton *)sender;
    NSArray* signalList=[XMLParser getSignalList:self.mapPoint.agency.agencyID subSystemID:self.mapPoint.subSystem.subSystemID timingPatternID:button.timingPattern.timingPatternID];
    int index=-1;
    for (int i=0; i<signalList.count; i++) {
        if ([self.mapPoint.intersection.intersectionID isEqualToString:[[signalList objectAtIndex:i] signalID]]) {
            index=i;
            break;
        }
    }
    if (index==-1) {
        return;
    }
    SignalDetailViewController *detailViewController=[[[SignalDetailViewController alloc] initWithNibNameAndSignal:@"SignalDetailViewController" agency:self.mapPoint.agency subSystem:self.mapPoint.subSystem timingPattern:button.timingPattern signalList:signalList signalIndex:index automaticPlayOrNot:NO bundle:nil] autorelease];
    [detailViewController setTitle:@"Signal Detail"];
    
    [self.viewController.navigationController pushViewController:detailViewController animated:YES];
    [self.viewController.navigationController setNavigationBarHidden:NO animated:YES];
}

@end
